package com.asura.framework.inst;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asura.framework.rulemodel.RuleInstOutModel;
import com.asura.framework.rulemodel.TreeNode;
import com.asura.framework.rulemodel.engine.dao.entity.RuleInstEntity;

public interface RuleInstConverter {

	public static class DefaultRuleInstConverter implements RuleInstConverter
	{

		Logger logger = LoggerFactory.getLogger( RuleInstConverter.class );

		@Override
		public RuleInstOutModel convert( RuleInstEntity ruleInst, TreeNode treeNode )
		{
			RuleInstOutModel outModel = new RuleInstOutModel( );
			{
				outModel.setRule_name( ruleInst.getRule_name( ) );
				outModel.setRule_value( ruleInst.getRule_value( ) );
				outModel.setBiz_id( ruleInst.getBiz_id( ) );
			}
			{
				outModel.setType( treeNode.getType( ) );
				outModel.setFront( treeNode.getFront( ) );
				outModel.setNote( treeNode.getNote( ) );
			}
			return outModel;
		}

		@Override
		public List< RuleInstOutModel > convert( List< RuleInstEntity > ruleInsts, List< TreeNode > treeNodes )
		{
			List< RuleInstOutModel > outModels = new ArrayList< RuleInstOutModel >( );
			for ( RuleInstEntity ruleInst : ruleInsts )
			{
				flag:
				{
					for ( TreeNode treeNode : treeNodes )
					{
						if ( ruleInst.getRule_name( ).equals( treeNode.getName( ) ) )
						{
							RuleInstOutModel outModel = convert( ruleInst, treeNode );
							outModels.add( outModel );
							break flag;
						}
					}
					logger.warn( "规则定义[" + ruleInst.getRule_name( ) + "]错误, 不符合规则模型规范. ignore it." );
				}
			}
			return outModels;
		}
	}

	RuleInstOutModel convert( RuleInstEntity ruleInst, TreeNode treeNode );

	List< RuleInstOutModel > convert( List< RuleInstEntity > ruleInsts, List< TreeNode > treeNodes );
}
